package com.wxhntmy;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.wxhntmy.config.Oauth2;
import com.wxhntmy.config.ProjectConfig;
import com.wxhntmy.google.oauth2.GoogleTranslateOauth2ByJsonUtil;
import com.wxhntmy.google.oauth2.GoogleTranslateOauth2Util;
import com.wxhntmy.google.v2.GoogleTranslateV2Utils;
import com.wxhntmy.google.v3.GoogleTranslateV3Utils;
import com.wxhntmy.google.v3.RomanizeTextSupportLanguageCode;
import com.wxhntmy.google.v3.SupportedLanguagesV3Code;
import com.wxhntmy.httpclient.RestMockByProxy;
import com.wxhntmy.redis.JedisTool;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.PrivateKey;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * GoogleTranslateV3测试类
 *
 * @author chenwc
 * @date 2024/7/30
 */
public class GoogleTranslateTest {
    private static final Logger logger = LoggerFactory.getLogger(GoogleTranslateTest.class);
    private static final Oauth2 OAUTH_2 = ProjectConfig.getInstance().getGoogle().getOauth2();

    @BeforeAll
    public static void beforeAll() {
        JedisTool.getJedis();
    }

    @AfterAll
    public static void afterAll() {
        JedisTool.close();
    }

    @Test
    @Tag("GenerateJwt")
    public void generateJwt() throws Exception {

        PrivateKey privateKey = GoogleTranslateOauth2Util.loadPrivateKeyFromJks(OAUTH_2.getJksStorePath(), OAUTH_2.getJksPassword(), OAUTH_2.getKeyAlias());
        logger.info("PrivateKey: {}", privateKey);
        String jwt = GoogleTranslateOauth2Util.generateJwt(privateKey);
        logger.info("JWT: {}", jwt);
        Map<String, String> form = new HashMap<>();
        form.put("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");
        form.put("assertion", jwt);
        String rst = RestMockByProxy.sendPostByFormBody(OAUTH_2.getTokenUrl(), null, form);
        logger.info("RST: {}", rst);
        logger.info("AccessToken: {}", JSONObject.parseObject(rst).getString("access_token"));
    }

    @Test
    @Tag("generateJwtJson")
    public void generateJwtJson() throws Exception {

        PrivateKey privateKey = GoogleTranslateOauth2ByJsonUtil.loadPrivateKeyFromJks();
        String jwt = GoogleTranslateOauth2ByJsonUtil.generateJwt(privateKey);
        logger.info("JWT: {}", jwt);
        Map<String, String> form = new HashMap<>();
        form.put("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");
        form.put("assertion", jwt);
        String rst = RestMockByProxy.sendPostByFormBody(OAUTH_2.getTokenUrl(), null, form);
        logger.info("RST: {}", rst);
        logger.info("AccessToken: {}", JSONObject.parseObject(rst).getString("access_token"));
    }

    @Test
    @Tag("AccessTokenIsExpire")
    public void accessTokenIsExpire() {
        boolean isExpire = GoogleTranslateOauth2Util.isExpireFromRedis();
        logger.info("AccessToken是否过期: {}", isExpire);
    }

    @Test
    @Tag("SetAccessTokenExpire")
    public void setAccessTokenExpire() {
        long expireTime = System.currentTimeMillis() / 1000 - 3600;
        logger.info("过期时间: {}", expireTime);
        GoogleTranslateOauth2Util.setExpireDateToRedis(expireTime);
    }

    @Test
    @Tag("TranslateBatchTextV2")
    public void translateBatchTextV2() {
        LinkedList<String> textList = new LinkedList<>();
        textList.add("你好");
        textList.add("世界");
        String targetLanguage = "en";
        Map<String, String> result = GoogleTranslateV2Utils.translateBatchText(textList, targetLanguage);
        logger.info("翻译结果: {}", result);
    }

    @Test
    @Tag("TranslateTextV2")
    public void translateTextV2() {
        String text = "Hello World!";
        String result = GoogleTranslateV2Utils.translateText(text, "zh-CN");
        logger.info("翻译结果: {}", result);
    }

    @Test
    @Tag("DetectLanguageV2")
    public void detectLanguageV2() {
        String text = "อ. เมืองระยอง";
        List<Map<String, String>> result = GoogleTranslateV2Utils.detectLanguage(text);
        logger.info("检测结果: {}", JSON.toJSONString(result));
        String languageCode = result.get(0).get("language");
        logger.info("语言代码: {}", languageCode);
        String translatedText = GoogleTranslateV2Utils.translateText(text, "zh-CN");
        logger.info("翻译结果: {}", translatedText);
    }

    @Test
    @Tag("ListSupportedLanguagesV2")
    public void listSupportedLanguagesV2() {
        List<String> result = GoogleTranslateV2Utils.listSupportedLanguages();
        logger.info("支持语言: {}", JSON.toJSONString(result));
    }

    @Test
    @Tag("listSrcLanguageSupportedLanguagesV2")
    public void listSrcLanguageSupportedLanguagesV2() {
        String srcLanguage = "zh-CN";
        JSONObject result = GoogleTranslateV2Utils.listSrcLanguageSupportedLanguages(srcLanguage);
        logger.info("支持语言: {}", JSON.toJSONString(result, JSONWriter.Feature.WriteMapNullValue));
    }

    @Test
    @Tag("TranslateTextV3")
    public void translateTextV3() {
        String text = "Hello World!";
        String result = GoogleTranslateV3Utils.translateText(text, SupportedLanguagesV3Code.EN, SupportedLanguagesV3Code.ZH_CN);
        logger.info("翻译结果: {}", result);
    }

    @Test
    @Tag("RomanizeTextV3")
    public void romanizeTextV3() {
        String text = "こんにちは世界";
        String result = GoogleTranslateV3Utils.romanizeText(RomanizeTextSupportLanguageCode.JA, text);
        logger.info("罗马音结果: {}", result);
    }

    @Test
    @Tag("DetectLanguageV3")
    public void detectLanguageV3() {
        String text = "こんにちは世界";
        List<Map<String, String>> result = GoogleTranslateV3Utils.detectLanguage(text);
        logger.info("检测结果: {}", JSON.toJSONString(result));
    }

    @Test
    @Tag("ListSupportedLanguagesV3")
    public void listSupportedLanguagesV3() {
        List<Map<String, String>> result = GoogleTranslateV3Utils.listSupportedLanguages();
        logger.info("支持语言: {}", JSON.toJSONString(result));
    }
}
